package com.simtop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.ModelData;
import com.simtop.pojo.Momo;
import com.simtop.pojo.User;
import com.simtop.pojo.wddata.*;
import com.simtop.service.ApiService;
import com.simtop.service.LayOutService;
import com.simtop.util.JwtUtil;
import com.simtop.util.RedisUtil;
import com.simtop.vo.StatusVo;
import com.simtop.vo.allTypeVo;
import com.simtop.vo.messageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lvyongwei
 * 前端U3D交互
 */
@Controller
@CrossOrigin
@RequestMapping("/unity")
public class U3dController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ApiService apiService;

    @Autowired
    private LayOutService layOutService;

    @PostMapping("/rustU3dData")
    @ResponseBody
    public ServerResponse rustU3dData(){
        try {
            Thread.currentThread().sleep(5000);
        }catch (Exception e){
        }
        //查询一跳最新data
        String lastlist = apiService.finNewDevice();
        JSONObject jsonObject = JSON.parseObject(lastlist);
        String rts = jsonObject.getString("rts");
  //      System.out.println(rts);
        List<WdDate> lastDate = JSON.parseArray(rts, WdDate.class);
     return    ServerResponse.createBySuccess(lastDate);


    }

    @PostMapping("/getU3dData")
    @ResponseBody
    public DzVo getU3dData(@RequestBody String data){

        try{
            Thread.currentThread().sleep(5000);
        }catch (Exception e){

        }
        //查询一跳最新data
      //  JSONObject jsonObject = JSONObject.parseObject(data);
        //Integer ids = jsonObject.getInteger("id");
        //通过id获取配置
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer status = jsonObject.getInteger("onlyStatus");
        //走编辑端
        if(status == 0){
            LayOutVosss finU3dData = layOutService.finU3dData();
            if(finU3dData == null){
                DzVo dzVo = new DzVo();
                dzVo.setId(0);
                dzVo.setLayoutName("");
                dzVo.setLayoutCon("");
                return dzVo;
            }
            DzVo dzVo = new DzVo();
            dzVo.setId(finU3dData.getId());
            dzVo.setLayoutName(finU3dData.getLayoutName());
            dzVo.setLayoutCon(finU3dData.getLayoutCon());
            return dzVo;
        }
        if(status == 1){
            //走展示端
            LayOutVosss finU3dDataas = layOutService.finU3dDataOne();
            if(finU3dDataas == null){
                DzVo dzVo = new DzVo();
                dzVo.setId(0);
                dzVo.setLayoutName("");
                dzVo.setLayoutCon("");
                return dzVo;
            }
            DzVo dzVo = new DzVo();
            dzVo.setId(finU3dDataas.getId());
            dzVo.setLayoutName(finU3dDataas.getLayoutName());
            dzVo.setLayoutCon(finU3dDataas.getLayoutCon());
            return dzVo;
        }


        DzVo dzVo = new DzVo();
        dzVo.setLayoutName(null);
        dzVo.setLayoutCon(null);

        return dzVo;



    }

    @PostMapping("/upU3dData")
    @ResponseBody
    public ServerResponse upU3dData(@RequestBody String data){
        //查询一跳最新data
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer ids = jsonObject.getInteger("id");
        String layoutCon = jsonObject.getString("layoutCon");
        //根据id更新新的布局配置
        layOutService.upU3dData(ids,layoutCon);
        return ServerResponse.createBySuccess();



    }

    @PostMapping("/updateU3dList")
    @ResponseBody
    public NewU3d updateU3dList(@RequestBody String data){
        //查询一跳最新data
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer id = jsonObject.getInteger("id");
        //变为0
        layOutService.Chaaa(id);

        String layoutName = jsonObject.getString("layoutName");
        String layoutCon = jsonObject.getString("layoutCon");
        //保存最新的布局返回 主键
        Date date = new Date();
        LayOut layOut = new LayOut();
        layOut.setLayoutName(layoutName);
        layOut.setLayoutCon(layoutCon);
        layOut.setEditStatus(1);
        layOut.setStartStatus(0);
        layOut.setLayoutuseTime(1);
        layOut.setLayoutuseStatus(0);
        layOut.setLayoutTime(date);
        layOut.setDelectStatus(0);
        layOut.setLayoutUser("管理员");
        layOut.setLayoutPlant("A6-厂房");
        layOutService.insertNew(layOut);


         //插入完毕以后根据 名称找到
try {
    int i = layOutService.findOneNameId(layoutName);
    NewU3d newU3d = new NewU3d();
    newU3d.setId(i);

    return newU3d;
}catch (Exception e){
    NewU3d newU3d = new NewU3d();

    return newU3d;
}




    }


    @PostMapping("/viewU3dData")
    @ResponseBody
    public ServerResponse viewU3dData(){

        try{
            List<OnlyType> alldeviceListAll = apiService.findAlldeviceListAll2();

            for (OnlyType as:alldeviceListAll
                 ) {
                //属于他的所有的标签页
                int id = as.getId();
                //根据id查询所有标签
                List<String> findAllTab =  apiService.findAllTab(id);

                as.setAllTab(findAllTab);
            }
            //找到所有的设备标签
            List<String> finAllU3dTab = apiService.finAllU3dTab();

            U3dAllVo u3dAllVo = new U3dAllVo();
            u3dAllVo.setOnlyType(alldeviceListAll);
            u3dAllVo.setAllTypeTab(finAllU3dTab);
            return ServerResponse.createBySuccess(u3dAllVo);
        } catch (Exception e){
           e.printStackTrace();
            return ServerResponse.createByErrorMsg("查询内容失败");
        }
    }


    @PostMapping("/safeData")
    @ResponseBody
    public ServerResponse safeData(@RequestBody String data){

        try{

            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer status = jsonObject.getInteger("status");
            if(status == 0){
                //关闭
                apiService.safeData(status);
                ServerResponse.createBySuccess("关闭安全模式成功");
            }
            if(status == 1){
                //开启
                apiService.safeData(status);
                ServerResponse.createBySuccess("开启安全模式成功");
            }



            ServerResponse.createBySuccess();

        } catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("查询内容失败");
        }
        return ServerResponse.createBySuccess();
    }



    @PostMapping("/getsafeData")
    @ResponseBody
    public allTypeVo getsafeData(){

        try{
          allTypeVo allTypeVos = apiService.getsafeData();





            return allTypeVos;

        } catch (Exception e){
            e.printStackTrace();

        }

        return  null;

    }

    @PostMapping("/getsafeDatas")
    @ResponseBody
    public ServerResponse getsafeDatas(){

        try{
            allTypeVo allTypeVos = apiService.getsafeData();





            return ServerResponse.createBySuccess(allTypeVos);

        } catch (Exception e){
            e.printStackTrace();

        }

        return  null;

    }

    @PostMapping("/massageStatus")
    @ResponseBody
    public ServerResponse massageStatus(@RequestBody String data){

        JSONObject jsonObject = JSONObject.parseObject(data);
        String cont = jsonObject.getString("cont");
        Integer status = jsonObject.getInteger("status");
        System.out.println(status);
        apiService.massageStatus(cont , status);


        return ServerResponse.createBySuccess();


    }




    @PostMapping("/getmassageStatus")
    @ResponseBody
    public ServerResponse getmassageStatus(){


        StatusVo statusVo =  apiService.getmassageStatus();



        return ServerResponse.createBySuccess(statusVo);

    }


    @PostMapping("/u3dgetmassageStatus")
    @ResponseBody
    public messageVo u3dgetmassageStatus(){


        messageVo messageVos =  apiService.getmassageStatus2();



        return messageVos;

    }


    /**
     * 记录场景信息
     * @param modelData
     * @param request
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ServerResponse add(@RequestBody ModelData modelData, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            redisUtil.set(u.getId() +":"+ u.getMillTime() + ":" + modelData.getId(),modelData,10*60*60*1000L);
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }

    /**
     * 张震
     * @param
     * @param
     * @return
     */
    @PostMapping("/addUmodel")
    @ResponseBody
    public ServerResponse addUmodel(@RequestBody Momo momo, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            redisUtil.set(u.getId() +":"+ u.getMillTime(),momo,10*60*60*1000L);
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }

    /**
     * 记录场景信息
     * @param request
     * @return
     */
    @PostMapping("/refresh")
    @ResponseBody
    public ServerResponse refresh(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            ModelData modelData = new ModelData(1, 0, 0);
            ModelData modelData1 = new ModelData(2, 0, 0);
            redisUtil.set(u.getId() +":"+ u.getMillTime() + ":" + 1,modelData,10*60*60*1000L);
            redisUtil.set(u.getId() +":"+ u.getMillTime() + ":" + 2,modelData1,10*60*60*1000L);
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }


    /**
     * 获取场景信息
     * @param modelData
     * @param request
     * @return
     */
    @PostMapping("/get")
    @ResponseBody
    public ServerResponse get(@RequestBody ModelData modelData, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            ModelData data = (ModelData) redisUtil.get(u.getId() +":"+ u.getMillTime() + ":" + modelData.getId());
            return ServerResponse.createBySuccess("获取内容成功",data);
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("获取内容失败");
        }
    }

    @PostMapping("/getMd")
    @ResponseBody
    public ServerResponse getMd( HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            Momo data = (Momo) redisUtil.get(u.getId() +":"+ u.getMillTime());
            return ServerResponse.createBySuccess("获取内容成功",data);
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("获取内容失败");
        }
    }
}
